package com.fand.platform.configuration.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.configuration.dto.VariableDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.platform.configuration.validator.VariableValidator;
import com.fand.platform.configuration.vo.VariableVo;
import com.fand.platform.configuration.service.ConfigurationService;
import com.google.common.collect.Maps;

/**
 * 变量
 *
 * @author yaoHT
 */
@Controller
public class VariableController {
	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	@Qualifier(VariableValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator variableValidator;

	@RequestMapping(value = { "/admin/configuration/variable/variable/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminVariableDelete(final VariableVo variableVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getVariableService().deleteVariableByIds(variableVo.getVariable());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable/edit.html" }, method = RequestMethod.GET)
	public String adminVariableEdit(final VariableVo variableVo, final Model model) {
		final VariableDto variable = this.configurationService.getVariableService().
				getVariableById(variableVo.getVariable());

		model.addAttribute("variable", variable);

		return "/admin/configuration/variable/variable/edit";
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable.html", "/admin/configuration/variable/variable/index.html" })
	public String adminVariableIndex() {
		return "/admin/configuration/variable/variable/index";
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminVariableIndexPagination(final VariableVo variableVo) {
		final PaginationDto<Map<String, Object>> variablePagination = this.configurationService.getVariableService()
				.paginationVariableBy1ForMapList(variableVo.getVariable());

		final Map<String, Object> variablePaginationMap = Maps.newHashMap();

		variablePaginationMap.put("total", variablePagination.getTotalSize());
		variablePaginationMap.put("rows", variablePagination.getDataList());

		return variablePaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable/new.html" }, method = RequestMethod.GET)
	public String adminVariableNew(final VariableVo variableVo, final Model model) {
		model.addAttribute("variable", variableVo.getVariable());

		return "/admin/configuration/variable/variable/new";
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminVariableSave(final VariableVo variableVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.variableValidator.validate(variableVo.getVariable(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getVariableService().saveVariableByIdOrCodeForVariable(variableVo.getVariable());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable/validationBy1.json" })
	public @ResponseBody
	Boolean adminVariableValidationBy1(final VariableVo variableVo) {
		try {
			final int count = this.configurationService.getVariableService().readVariableByCodeNqIdForInteger(
					variableVo.getVariable());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/variable/variable/view.html" }, method = RequestMethod.GET)
	public String adminVariableView(final VariableVo variableVo, final Model model) {
		final VariableDto variable = this.configurationService.getVariableService().
					getVariableById(variableVo.getVariable());

		model.addAttribute("variable", variable);

		return "/admin/configuration/variable/variable/view";
	}
}
